<!DOCTYPE html>
<title>Test event dispatches and attribute changes for seek to duration.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<video></video>
<script>
async_test(function(t) {
    var video = document.querySelector("video");

    var watcher = new EventWatcher(t, video, ["durationchange", "loadedmetadata",
        "loadeddata", "seeking", "timeupdate", "seeked", "pause", "ended"]);

    watcher.wait_for(["durationchange", "loadedmetadata", "loadeddata"]).then(t.step_func(function() {
        // Seek to the duration of the media.
        assert_less_than(video.currentTime, video.duration);
        assert_false(video.ended)
        assert_false(video.paused);
        //Starting seek to duration by setting video.currentTime to video.duration.
        video.currentTime = video.duration;
        testCommonAttributes({'seeking': true, 'ended': false});
        return watcher.wait_for("seeking");
    })).then(t.step_func(function() {
        testCommonAttributes({'seeking': true, 'ended': false});
        assert_false(video.paused);
        return watcher.wait_for("timeupdate");
    })).then(t.step_func(function() {
        // Exactly when 'video.ended' will become true may vary between the
        // start of the seek until the pause for ended.
        testCommonAttributes({'seeking': false});
        return watcher.wait_for("seeked");
    })).then(t.step_func(function() {
        testCommonAttributes({'seeking': false});
        return watcher.wait_for("timeupdate");
    })).then(t.step_func(function() {
        testCommonAttributes({'seeking': false});
        return watcher.wait_for("pause");
    })).then(t.step_func(function() {
        assert_true(video.paused, "should be paused after seeking");
        testCommonAttributes({'seeking': false, 'ended': true});
        return watcher.wait_for("ended");
    })).then(t.step_func_done(function() {
        testCommonAttributes({'seeking': false, 'ended': true});
        assert_true(video.paused, "should be paused upon ended");
    }));

    function testCommonAttributes(testExpectations) {
        assert_equals(video.seeking, testExpectations.seeking);
        if ('ended' in testExpectations)
            assert_equals(video.ended, testExpectations.ended);
        assert_equals(video.currentTime, video.duration);
    }

    video.src = "resources/test.ogv";;
    video.play();
});
</script>